package com.miyu.client1.test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class hump {
    private static Pattern linePattern = Pattern.compile("_(\\w)");

    /** 下划线转驼峰 */
    public static String lineToHump(String str) {
        str = str.toLowerCase();
        Matcher matcher = linePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    /** 驼峰转下划线(简单写法，效率低于{@link #humpToLine2(String)}) */
    public static String humpToLine(String str) {
        return str.replaceAll("[A-Z]", "_$0").toLowerCase();
    }

    private static Pattern humpPattern = Pattern.compile("[A-Z]");

    /** 驼峰转下划线,效率比上面高 */
    public static String humpToLine2(String str) {
        Matcher matcher = humpPattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    public static void main(String[] args) {
        String lineToHump = lineToHump(",ID_CODE\n" +
                ",EMP_IN_DAY\n" +
                ",EMP_BIRTHDAY\n" +
                ",EMP_SEX\n" +
                ",EMAIL\n" +
                ",MOBILE_PHONE\n" +
                ",WORK_PHONE\n" +
                ",IS_STOCK\n" +
                ",SPELL\n" +
                ",SCHOOL\n" +
                ",PAY_WAY\n" +
                ",PAY_WAY_CN\n" +
                ",IS_PAY\n" +
                ",BANK_ACCOUNT\n" +
                ",PEOPLE\n" +
                ",PEOPLE_CN\n" +
                ",DUTY_DAY\n" +
                ",TITLE_DAY\n" +
                ",FIRSTJOB_DAY\n" +
                ",SLRY_BDATE\n" +
                ",DEAL_DATE\n" +
                ",COMEFROM_CN\n" +
                ",EMP_AGE\n" +
                ",TITLE_CODE\n" +
                ",DUTY_CODE\n" +
                ",EMP_TYPE_CODE\n" +
                ",EMP_POL_CODE\n" +
                ",EMP_TIME_CODE\n" +
                ",PROFESSIONAL\n" +
                ",PROFESSIONAL_CN\n");
        System.out.println(lineToHump);// fParentNoLeader
        System.out.println(humpToLine(lineToHump));// f_parent_no_leader
        System.out.println(humpToLine2(lineToHump));// f_parent_no_leader
    }
}
